package com.ibilibili.download;

import com.ibilibili.utils.MysqlClient;
import com.ibilibili.utils.Consts;

import java.util.ArrayList;
import java.util.HashMap;



public class Main {
    public static void main(String[] args) {
        String downloadPath = "/tmp";
        if (args.length == 1) {
            downloadPath = args[0];
        }

        try {
            MysqlClient mysql = new MysqlClient("jdbc:mysql://localhost:3306/recommendDB?useSSL=true&useUnicode=true&characterEncoding=gb2312", "root", "xxx");
//            MysqlClient mysql = new MysqlClient("jdbc:mysql://192.168.1.103:3306/recommendDB?useSSL=true&useUnicode=true&characterEncoding=gb2312", "root", "xxx");
            ArrayList res = mysql.select("select aid from bili_crawl order by favorites desc limit 3");
            for (int i = 0; i < res.size(); i++) {
                HashMap<String, String> item = (HashMap<String, String>)res.get(i);
                for (String v: item.values()) {
                    System.out.println("========================== download " + (i + 1 ) + "=============================");
                    String downloadUrl = Consts.DOWNLOAD_URL_PREFIX + v;
                    String downloadFileName = downloadPath + "/" + v + ".mp4";
                    System.out.println("downloadUrl: " + downloadUrl);
                    System.out.println("downloadFileName: " + downloadFileName);
                    VideoDownload vd = new VideoDownload(downloadUrl, downloadFileName);
                    vd.down();
                    Thread.sleep(100);
                }
            }

        }catch (Exception e) {
            e.printStackTrace();
        }
    }
}
